package com.fckbug.service.impl;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fckbug.Constants;
import com.fckbug.QaType;
import com.fckbug.dao.QADAO;
import com.fckbug.service.QAService;
import com.fckbug.util.TimeUtil;
import com.fckbug.vos.ContentVO;
import com.fckbug.vos.DetailVO;

public class QAServiceImpl implements QAService {
	private QADAO qaDAO;

	public QADAO getQaDAO() {
		return qaDAO;
	}

	public void setQaDAO(QADAO qaDAO) {
		this.qaDAO = qaDAO;
	}

	public Integer saveQuestion(String title, String content, int userid) {
		Integer qId = qaDAO.saveQ(title, content, userid);
		if(qId != null) {
			if(qaDAO.saveQT(qId) != 1 ) return null;
		}
		return qId;
	}

	public DetailVO getDetail(String qId) {
		DetailVO detailVO = new DetailVO();
		try {
			Integer bi = Integer.valueOf(qId);
			List list = qaDAO.getQ(bi);
			if(list.size()!=0) {
				ContentVO question = new ContentVO();
				question.setId(bi);
				question.setType(QaType.QUESTION);
				Object[] objs = (Object[])list.get(0);
				question.setTitle((String)objs[0]);
				question.setContent((String)objs[1]);
				Timestamp tt = (Timestamp)objs[2];
				question.setCtTime(new Date(tt.getTime()));
				question.setCreateUserId((Integer)objs[3]);
				setQTime(question);
				setComment(question);
				detailVO.setQuestion(question);
				detailVO.setAnswers(getAnswers(bi));
				return detailVO;
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean saveAnswer(String qId, String content, int userid) {
		try {
			Integer bi = Integer.parseInt(qId);
			int aId = qaDAO.saveA(bi, content, userid);
			if(aId>-1){
				if(qaDAO.saveAT(aId) == 1) {
					return true;
				}
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}

	public void setQTime(ContentVO question) {
		List list = qaDAO.getQTime(question.getId());
		if(list.size() == 1) {
			Object[] objs = (Object[])list.get(0);
			question.setMarkTimes((Integer)objs[0]);
			question.setFavoriteTimes((Integer)objs[1]);
			question.setViewTimes((Integer)objs[2]);
		}
	}
	
	public void setComment(ContentVO c) {
		List list = qaDAO.getComment(c.getType(), c.getId());
		List<ContentVO> comments = new  ArrayList<ContentVO>();
		for(int i = 0; i<list.size();i++) {
			ContentVO one = new ContentVO();
			Object[] objs = (Object[])list.get(i);
			one.setContent((String)objs[0]);
			one.setCreateUserId((Integer)objs[1]);
			Timestamp tt = (Timestamp)objs[2];
			one.setCtTime(new Date(tt.getTime()));
			comments.add(one);
		}
		c.setComments(comments);
	}
	
	public List<ContentVO> getAnswers(int qId) {
		List list =qaDAO.getA(qId);
		List<ContentVO> answers = new ArrayList<ContentVO>();
		for(int i=0;i<list.size();i++) {
			ContentVO one = new ContentVO();
			Object[] objs = (Object[])list.get(i);
			one.setId((Integer)objs[0]);
			one.setType(QaType.ANSWER);
			one.setContent((String)objs[1]);
			one.setCreateUserId((Integer)objs[2]);
			setComment(one);
			answers.add(one);
		}
		return answers;
	}

	@Override
	public List getATime(Integer aId) {
		return qaDAO.getATime(aId);
	}

	@Override
	public int alterATime(int userId, String aId, String increment) {
		try {
			int a = Integer.parseInt(aId);
			int b = Integer.parseInt(increment);
			if(qaDAO.saveUserAMark(userId, a) == 1) {//保存答案是谁mark过的
				if(qaDAO.alterATime(a, b) == 1) {
					return 1;
				}
			} else {//重复了
				return 0;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return -1;
	}

	@Override
	public int alterQTime(int userId, String aId, String increment) {
		try {
			int a = Integer.parseInt(aId);
			int b = Integer.parseInt(increment);
			if(qaDAO.saveUserQMark(userId, a) == 1) {//保存问题是谁mark过的
				if(qaDAO.alterQTime(a, b) == 1) {
					return 1;
				}
			} else {//重复了
				return 0;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return -1;
	}

	@Override
	public int alterFTime(int userId, String aId, String increment) {
		try {
			int a = Integer.parseInt(aId);
			int b = Integer.parseInt(increment);
			if(qaDAO.saveUserFMark(userId, a) == 1) {//保存问题是谁mark过的
				if(qaDAO.alterFTime(a, b) == 1) {
					return 1;
				}
			} else {//重复了
				return 0;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return -1;
	}

	@Override
	public boolean alterQVTime(String qId) {
		boolean result = false;
		try {
			int q = Integer.parseInt(qId);
			int a = qaDAO.alterQVTime(q);
			if(a == 1) {
				result = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean saveComment(String qaId, String type, String content, int userid) {
		boolean isOk = false;
		try {
		int a = Integer.parseInt(qaId);
		int b = Integer.parseInt(type);
		int result = qaDAO.saveC(a, b, content, userid);
		if(result == 1) {
			isOk = true;
		}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		return isOk;
	}

	@Override
	public List<ContentVO> getUnAnsweredQ(int start) {
		List list = qaDAO.getUnAnsweredQ(start, Constants.PAGE_30);
		List<ContentVO> result = new ArrayList<ContentVO>();
		for(int i=0;i<list.size();i++) {
			ContentVO one = new ContentVO();
			Object[] objs = (Object[])list.get(i);
			one.setId((Integer)objs[0]);
			one.setTitle((String)objs[1]);
			one.setCreateUserId((Integer)objs[2]);
			Timestamp tt = (Timestamp)objs[3];
			one.setCtTime(tt==null?null:new Date(tt.getTime()));
			one.setMarkTimes((Integer)objs[4]);
			one.setViewTimes((Integer)objs[5]);
			one.setAnswerCount(((BigInteger)objs[6]).intValue());
			result.add(one);
		}
		return result;
	}

	@Override
	public List<ContentVO> getAllQ(int start) {
		List list = qaDAO.getAllQ(start, Constants.PAGE_30);
		List<ContentVO> result = new ArrayList<ContentVO>();
		for(int i=0;i<list.size();i++) {
			ContentVO one = new ContentVO();
			Object[] objs = (Object[])list.get(i);
			one.setId((Integer)objs[0]);
			one.setTitle((String)objs[1]);
			one.setCreateUserId((Integer)objs[2]);
			Timestamp tt = (Timestamp)objs[3];
			one.setCtTime(tt==null?null:new Date(tt.getTime()));
			one.setMarkTimes((Integer)objs[4]);
			one.setViewTimes((Integer)objs[5]);
			one.setAnswerCount(((BigInteger)objs[6]).intValue());
			result.add(one);
		}
		return result;
	}

}
